package org.kfive.nova.io.grizzly.echo;

import org.glassfish.grizzly.filterchain.FilterChainBuilder;
import org.glassfish.grizzly.filterchain.TransportFilter;
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
import org.glassfish.grizzly.nio.transport.TCPNIOTransportBuilder;
import org.glassfish.grizzly.utils.StringFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * TODO
 *
 * @author zhanwu.lzw
 * @date 2018年01月25日 14:11
 */
public class EchoServer {
    
    /**
     * the log
     */
    public static final Logger LOGGER = LoggerFactory.getLogger(EchoServer.class);

    public static final String HOST ="localhost";

    public static final int PORT = 7777 ;

    public static void main(String[] args) {
        FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();

        filterChainBuilder.add(new TransportFilter());

        filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));

        filterChainBuilder.add(new EchoFilter());

        final TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build();

        transport.setProcessor(filterChainBuilder.build());

        try{

            transport.bind(HOST,PORT);

            transport.start();

            LOGGER.info("Press any key to stop the server");

            System.in.read();
        } catch (IOException e) {

            LOGGER.info("error:{}",e);

        } finally {

            LOGGER.info("Stopping transport...");

            transport.shutdown();

            LOGGER.info("Stopped transport...");
        }
    }
    
}
